package com.maple.soa.controller;

import com.maple.soa.pojo.User;
import com.maple.soa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author lijian
 * @date 2020/7/15
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String index(Model model) {

        return "user/index";
    }

    @RequestMapping("/listUser")
    @ResponseBody
    public List<User> listUser() {
        List<User> users = userService.listAll();
        return users;
    }

    @RequestMapping("/save")
    @ResponseBody
    public User save(String account) {

        User user = new User();
        user.setUniqueCode(UUID.randomUUID().toString().replaceAll("-", ""));
        user.setAccount(account);
        user.setDeleted(false);
        user.setCreateTime(new Date());
        userService.save(user);

        return user;
    }

}
